package ownserver;

import java.io.Serializable;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.sun.sgs.app.AppListener;
import com.sun.sgs.app.ClientSession;
import com.sun.sgs.app.ClientSessionListener;
import com.sun.sgs.app.DataManager;
import com.sun.sgs.app.TaskManager;
import com.sun.sgs.app.AppContext;
import com.sun.sgs.app.ManagedReference;
import com.sun.sgs.app.ManagedObject;
import com.sun.sgs.app.util.ScalableHashMap;
import com.sun.sgs.app.util.ScalableList;

/**
 * Simple example of listening for user {@linkplain AppListener#loggedIn login}
 * in the Project Darkstar Server.
 * <p>
 * Logs each time a user logs in, and sets their listener to a
 * new {@link HelloEchoSessionListener}.
 */
public class TestApp
    implements AppListener, // to get called during startup and login.
               Serializable,
               ManagedObject // since all AppListeners are ManagedObjects.
{
    /** The version of the serialized form of this class. */
    private static final long serialVersionUID = 1L;

    /** The {@link Logger} for this class. */
    private static final Logger logger =
        Logger.getLogger(TestApp.class.getName());

    private ManagedReference<World> worldRef = null;
    
    private ManagedReference<ScalableHashMap<String, Integer>> playerNamesToIDsRef = null;
    
    private int playerCount;

    /** {@inheritDoc} */
    public void initialize(Properties props) {
    	World world = new World();
    	ScalableHashMap<String, Integer> map = new ScalableHashMap<String, Integer> ();
    	playerCount = 0;
    	
    	DataManager dataManager = AppContext.getDataManager();
        dataManager.markForUpdate(this);
        
        worldRef = dataManager.createReference(world);
        playerNamesToIDsRef = dataManager.createReference(map);
        
        world.createObstacleShips(playerCount++, 5, 2, 1.0);
    }

    /**
     * {@inheritDoc}
     * <p>
     * Logs a message each time a new session logs in.
     */
    public ClientSessionListener loggedIn(ClientSession session) {
    	ScalableHashMap<String, Integer> map = playerNamesToIDsRef.get();
    	String name = session.getName(); // name of the player
    	
    	// TODO: koko userid-juttu pitäisi itseasiassa kiinnittää ClientSession:iin
    	
    	int userid = -1;
    	
    	if (map.containsKey(name))
    	{
    		userid = map.get(name);
    	}
    	else
    	{
    		DataManager dataManager = AppContext.getDataManager();
            dataManager.markForUpdate(this);
            
    		userid = playerCount + 1;
    		playerCount++;
    		
    		map.put(name, userid);
    	}
    	
        // User has logged in
        logger.log(Level.INFO, "User {0} has logged in", session.getName());

        
        // Delegate to a factory method on Player,
        // since player management really belongs in that class.
        Player player = Player.loggedIn(session, worldRef, userid);

        // return player object as listener to this client session
        return player;
        
        // Return a valid listener
        //return new HelloEchoSessionListener(session);
    }
}

